package com.onjob.recruit;

import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import util.dao.Pagination;

import com.opensymphony.xwork2.ActionSupport;

public class CompanyAction extends ActionSupport {

	/**
	 * 和公司信息相关的action
	 */
	private static final long serialVersionUID = 1L;
	private List<Company> companyList;
	private String id;
	private Company company;
	private CompanyDao companyDao;
	private Pagination<Company> pagination;
	private String jsonString;
	private String returnMassage;

	public String getReturnMassage() {
		return returnMassage;
	}

	public void setReturnMassage(String returnMassage) {
		this.returnMassage = returnMassage;
	}

	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}

	public Pagination<Company> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<Company> pagination) {
		this.pagination = pagination;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public List<Company> getCompanyList() {
		return companyList;
	}

	public void setCompanyList(List<Company> companyList) {
		this.companyList = companyList;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public CompanyDao getCompanyDao() {
		return companyDao;
	}

	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public String beginCompany() {
		if (id != null && !id.isEmpty()) {
			this.setCompany(companyDao.findById(id));
		}
		return SUCCESS;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public String editCompanyA() {
		companyDao.saveOrUpdate(this.company);
		return SUCCESS;
	}

	public void validateEditCompanyA() {
		if (companyDao.companyNameIsExist(company.getCompanyName())) {
			this.addFieldError("company.companyName", this.getText("公司名称已存在"));
		}
	}

	public String deleteComany() {
		Company com = new Company();
		com.setId(id);
		companyDao.delete(com);
		return SUCCESS;
	}

	public String findCompany() {
		company = companyDao.findById(id);
		return SUCCESS;
	}

	public String browse() {
		if (pagination == null) {
			pagination = new Pagination<Company>();
		}
		if (pagination.getSize() == 0) {
			pagination.setSize(3);
		}
		if (pagination.getNo() == 0) {
			pagination.setNo(1);
		}
		companyDao.browse(pagination);
		return SUCCESS;
	}

	public String checkCompanyName() {
		if (this.companyDao.companyNameIsExist(jsonString)) {
			this.returnMassage = "公司名称已存在";
		} else {
			this.returnMassage = "公司名称可用";
		}
		return SUCCESS;
	}

}
